<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{total}} 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="openDrawer('add')"
            round>
            添加面经
          </el-button>
        </div>
      </template>

      <!-- 需求一:新增一个性别列
           需求二:新增一个删除列
           需求三:性别数据 -->
       <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="stem"
        label="标题"
        width="400">
      </el-table-column>
      <el-table-column
        prop="creator"
        label="作者">
      </el-table-column>
      <el-table-column
        prop="likeCount"
        label="点赞">
      </el-table-column>
      <el-table-column
        prop="views"
        label="浏览数">
        </el-table-column>
         <el-table-column
        prop="createdAt"
        label="更新时间">
        </el-table-column>
        <!-- 作用域插槽
        <template v-slot="{row}">
          {{row.gender ?'男':'女'}}
        </template>
      </el-table-column> -->
      <!--  -->
      <el-table-column
      width="200"
        label="操作">
        <!-- 这里是插槽的位置 -->
        <template v-slot="{row}">
          <!-- <el-button type="danger" size="mini">删除</el-button>
           -->
             <div class="actions">
        <i class="el-icon-view" @click="openDrawer('preview',row.id)"></i>
        <i class="el-icon-edit-outline" @click="openDrawer('edit',row.id)"></i>
        <i class="el-icon-delete" @click="hDel(row.id)"></i>
      </div>
        </template>
      </el-table-column>

    </el-table>
    <el-pagination
    @current-change="hChange"
       background
       layout="prev, pager, next"
       :pageSize="pageSize"
       :total="total">
</el-pagination>
<!-- 抽屉 -->
    </el-card>
    <el-drawer
  :visible.sync="isShowDrawer"
  :title="drawerTitle"
  direction="rtl"
  size="50%"
>
 <el-form ref="form" :model="form" :rules="rules" label-width="80px">
  <el-form-item label="标题" prop="stem">
    <el-input v-model="form.stem" placeholder="输入面经标题"></el-input>
  </el-form-item>
  <el-form-item label="内容" prop="content">
    <quill-editor @blur="$refs.form.validateField('content')" v-model="form.content"></quill-editor>
  </el-form-item>
  <el-form-item>
    <el-button @click="hSubmit" type="primary">确认</el-button>
    <el-button>取消</el-button>
  </el-form-item>
</el-form>
</el-drawer>
  </div>
</template>

<script>
import { addArticle, getArticleList, delArticle, getArticle } from '@/api/article'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      list: [],
      total: 0,
      pageSize: 10,
      current: 1,
      isShowDrawer: false,
      drawerType: '',
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          { required: true, message: '请输入面经标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入面经内容', trigger: 'blur' }
        ]
      }
    }
  },

  created () {
    // 页面加载就要调用获取数据的方法
    this.loadData() ~``
  },
  methods: {
    async loadData () {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      this.total = res.data.total
      this.list = res.data.rows
    },
    async hDel (id) {
      // console.log(id)
      await delArticle({ id })
      this.$message.success('删除成功')
      this.loadData()
    },
    hChange (current) {
      // 修改当前页
      this.current = current
      // 重新加载数据=>覆盖
      this.loadData()
    },
    async openDrawer (type, id) {
      // console.log(type, id)
      this.isShowDrawer = true
      this.drawerType = type
      if (type === 'edit') {
        const res = await getArticle({ id })
        this.form.stem = res.data.stem
        this.form.content = res.data.content
      }
    },
    hSubmit () {
      // 1.兜底校验
      this.$refs.form.validate(async valid => {
        if (!valid) return
        // 2.通过校验就发请求
        await addArticle(this.form)
        // 3.提醒用户添加成功
        this.$message.success('添加面经成功')
        // 4.关闭抽屉
        this.isShowDrawer = false
        // 5.重新获取数据
        this.current = 1
        this.loadData()
      })
    }
  },
  computed: {
    drawerTitle () {
      const obj = {
        add: '添加面经',
        preview: '面经预览',
        edit: '修改面经'
      }
      return obj[this.drawerType]
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
